package com.vr.core.system.apiInfo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vr.core.system.apiInfo.apiInfo.SlCoreApi;
import com.vr.core.system.apiInfo.service.SlCoreApiService;
import com.vr.core.system.tableInfo.service.SlCoreTableService;
import com.vr.core.system.tableInfo.tableInfo.SlCoreTable;
import com.vr.mybatis.dynamic.DBContextHolder;
import com.vr.mybatis.dynamic.DataSourceManager;
import com.vr.mybatis.dynamic.DynamicDataSource;
import com.vr.mybatis.service.DbConnInfoService;
import com.vr.tool.api.R;
import com.vr.tool.util.BaseQuery;
import com.vr.tool.util.SelectConditionUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description TODO
 * @Author kenson
 * @Date 2024/12/7 0:06
 */
@RequestMapping("/apiManager")
@RestController
@AllArgsConstructor
public class ApiManagerController {
    private final SlCoreApiService service;
private final DbConnInfoService dbConnInfoService;

    @PostMapping("pageList")
    public R<IPage<SlCoreApi>> listPage(@RequestBody BaseQuery<SlCoreApi> query, HttpServletRequest request){
        String sourceKey = request.getHeader("sourceKey");
        if(StrUtil.isNotBlank(sourceKey)){
            dbConnInfoService.changeDb(sourceKey);
        }
        IPage page = service.page(query.getPage(), SelectConditionUtil.parseWhereSql(query.getConditions()));
        dbConnInfoService.resetDb();
        return R.ok(page);
    }

    @PostMapping("save")
    public R<Boolean> save(@RequestBody SlCoreApi sCoreApi){
        boolean save = service.save(sCoreApi);
        return R.ok(save);
    }

    @PostMapping("update")
    public R<Boolean> update(@RequestBody SlCoreApi slCoreApi){
        boolean save = service.updateById(slCoreApi);
        return R.ok(save);
    }

    @PostMapping("delete/{id}")
    public R<Boolean> update(@PathVariable("id") String id){
        boolean save = service.removeById(id);
        return R.ok(save);
    }

}


